﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace firstCodeFirstApp
{
    class Program
    {
        static void Main(string[] args)
        {

            Database.SetInitializer(new Initializer());//设置初始化器

            using (var context = new Context())
            {
                //context.Database.CreateIfNotExists();

                //var donators = new List<Donator>() 
                //{ 
                //new Donator(){ Name="郭靖", Amount = 50, DonateDate=new DateTime(2016,4,7)},
                //new Donator(){ Name="黄蓉", Amount = 5, DonateDate=new DateTime(2016,4,7)},
                //new Donator(){ Name="黄老邪", Amount = 18.8m, DonateDate=new DateTime(2016,4,7)}
                //};

                //context.Donators.AddRange(donators);
                //context.SaveChanges();


                //var ds = context.Donators;
                //Console.WriteLine("");

                //foreach (var item in ds)
                //{
                //    Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.DonatorId, item.Name, item.Amount, item.DonateDate);
                //}

                //if (ds.Any()) // 有元素
                //{
                //    var g = ds.First(d=>d.Name == "黄蓉");
                //    g.Name = "黄蓉改名字了";
                //    context.SaveChanges();
                //}

                //foreach (var item in ds)
                //{
                //    Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.DonatorId, item.Name, item.Amount, item.DonateDate);
                //}

                //if (ds.Any())
                //{
                //    var toBeDel = ds.Single(d => d.Name == "待打赏");

                //    if (toBeDel != null)
                //    {

                //        context.Donators.Remove(toBeDel);
                //        context.SaveChanges();
                //    }
                //}

                //var donator = new Donator()
                //{

                //    Name = "键盘里的鼠标",
                //    Amount = 6,
                //    DonateDate = DateTime.Parse("2016.6.6"),


                //};

                //donator.PayWays.Add(new PayWay() { Name = "支付宝" });
                //donator.PayWays.Add(new PayWay() { Name = "微信" });

                //context.Donators.Add(donator);

                //context.SaveChanges();

                #region 6.1 一对多关系 例子2

                //var donatorType = new DonatorType
                //{
                //    Name = "博客园园友",
                //    Donators = new List<Donator>
                //    {
                //        new Donator
                //        {
                //            Amount =6,Name = "键盘里的鼠标",DonateDate =DateTime.Parse("2016-4-13"),
                //            PayWays = new List<PayWay>{new PayWay{Name = "支付宝"},new PayWay{Name = "微信"}}
                //        }     
                //    }
                //};
                //var donatorType2 = new DonatorType
                //{
                //    Name = "非博客园园友",
                //    Donators = new List<Donator>
                //    {

                //         new Donator
                //        {
                //            Amount =10,Name = "待赞助",DonateDate =DateTime.Parse("2016-4-27"),
                //            PayWays = new List<PayWay>{new PayWay{Name = "支付宝"},new PayWay{Name = "微信"}}
                //        }
        
                //    }
                //};
                //context.DonatorTypes.Add(donatorType);
                //context.DonatorTypes.Add(donatorType2);
                //context.SaveChanges();

                #endregion


                #region 7 一对一关系

                //var student = new Student
                //{
                //    CollegeName = "XX大学",
                //    EnrollmentDate = DateTime.Parse("2011-11-11"),
                //    Person = new Person
                //    {
                //        Name = "Farb",
                //    }
                //};

                //context.Students.Add(student);
                //context.SaveChanges();

                #endregion

                #region 8 多对多关系

                var person = new Person
                {
                    Name = "比尔盖茨",
                };
                var person2 = new Person
                {
                    Name = "乔布斯",
                };
                context.People.Add(person);
                context.People.Add(person2);
                var company = new Company
                {
                    CompanyName = "微软"
                };
                company.Persons.Add(person);
                context.Companies.Add(company);
                context.SaveChanges();

                #endregion


            }

            Console.WriteLine("Creation finished.");
            Console.ReadKey();

        }
    }
}
